package com.example.myblog1_0.service.Impl;

import com.example.myblog1_0.mapper.CommentMapper;
import com.example.myblog1_0.mapper.UserMapper;
import com.example.myblog1_0.model.Comment;
import com.example.myblog1_0.model.MyUser;
import com.example.myblog1_0.service.CommentService;
import com.example.myblog1_0.service.UserService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: xc
 * @Date: 2019/1/22 20:39
 * @Description:
 **/
@Service
public class CommentServiceImpl implements CommentService{
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    UserMapper userMapper;
    @Override
    public JSONObject saveComment(Comment comment) {
        JSONObject jsonObject = new JSONObject();
        if (commentMapper.saveComment(comment)){
            jsonObject.put("status",200);
            jsonObject.put("message","存储成功");
            return jsonObject;
        }
        else{
            jsonObject.put("status",403);
            jsonObject.put("message","存储失败");
            return jsonObject;
        }
    }

    @Override
    public JSONObject getCommentByID(String id) {
        JSONObject returnObject = new JSONObject();
        List<Comment> comments = commentMapper.getCommentByID(id);
        if (comments!=null) {
            JSONArray result = new JSONArray();
            for (Comment comment : comments) {
                JSONObject jsonObject = new JSONObject();
                MyUser myUser =userMapper.findUserInfoByName(comment.getUserID());
                jsonObject.put("id",comment.getId());
                jsonObject.put("img",myUser.getAvatarUrl());
                jsonObject.put("userID",myUser.getUsername());
                jsonObject.put("replyID",comment.getReplyID());

                jsonObject.put("replyName",myUser.getNickname());
                jsonObject.put("time", comment.getSendTime());
                jsonObject.put("content", comment.getContent());
                jsonObject.put("osName",comment.getOsName());
                result.add(jsonObject);
            }
            returnObject.put("status", 200);
            returnObject.put("result", result);
            return returnObject;
        }else{
            returnObject.put("status",404);
            returnObject.put("message","此篇文章还没有评论");
            return returnObject;
        }
    }

    @Override
    public JSONObject getReplyByCommentID(String id) {
        List<Comment> replies = commentMapper.getReplyByCommentID(id);
        if (replies.size()!=0){
            JSONArray results = new JSONArray();
            for (Comment comment:replies){
                MyUser currentUser = userMapper.findUserInfoByName(comment.getUserID());

                MyUser lastUser = userMapper.findUserInfoByName(comment.getLastUserID());
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("userID",currentUser.getUsername());
                jsonObject.put("replyName",currentUser.getNickname());
                jsonObject.put("beReplyCommentID",comment.getReplyID());
                jsonObject.put("beReplyID",lastUser.getUsername());
                jsonObject.put("beReplyName",lastUser.getNickname());
                jsonObject.put("content",comment.getContent());
                jsonObject.put("time",comment.getSendTime());
                jsonObject.put("osName",comment.getOsName());
                results.add(jsonObject);
            }

            JSONObject returnObject = new JSONObject();
            returnObject.put("status",200);
            returnObject.put("result",results);
            return returnObject;
        }else {
            JSONObject returnObject = new JSONObject();
            returnObject.put("status",404);
            return returnObject;
        }
    }
}
